package com.smile.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.smile.model.dto.request.*;
import com.smile.service.mqtt.handler.AbstractServicesHandler;
import com.smile.service.FlyService;
import com.smile.service.entity.services.BaseServicesEntity;
import com.smile.service.entity.services.FlightAuthorityGrabData;
import com.smile.service.task.FlyServicesExecutor;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.UUID;

@Service
public class FlyServiceImpl implements FlyService {


    @Autowired
    private AbstractServicesHandler abstractServicesHandler;

    private Integer defaultValue = 1024;
    private Integer step = 660;


    @Override
    public void flyToPoint(FlyToPointRequestDTO flyToPointRequestDTO) throws MqttException {
        FlyServicesExecutor
                .create()
                .flyToPoint(flyToPointRequestDTO.getMaxSpeed()
                        , flyToPointRequestDTO.getHeight()
                        , flyToPointRequestDTO.getLatitude()
                        , flyToPointRequestDTO.getLongitude())
                .gateway(flyToPointRequestDTO.getGateway()).execute();
    }

    @Override
    public void takeoffToPoint(TakeOffToPointRequestDTO takeOffToPointRequestDTO) throws MqttException {
        FlyServicesExecutor
                .create()
                .flightId(takeOffToPointRequestDTO.getFlightId())
                .takeoffToPoint(
                        takeOffToPointRequestDTO.getCommanderFlightHeight(),
                        takeOffToPointRequestDTO.getCommanderModeLostAction(),
                        takeOffToPointRequestDTO.getMaxSpeed(),
                        takeOffToPointRequestDTO.getCommanderFlightMode(),
                        takeOffToPointRequestDTO.getRthMode(),
                        takeOffToPointRequestDTO.getRcLostAction(),
                        takeOffToPointRequestDTO.getRthAltitude(),
                        takeOffToPointRequestDTO.getTargetHeight(),
                        takeOffToPointRequestDTO.getSecurityTakeoffHeight(),
                        takeOffToPointRequestDTO.getTargetLongitude().setScale(6, RoundingMode.HALF_UP),
                        takeOffToPointRequestDTO.getTargetLatitude().setScale(6, RoundingMode.HALF_UP)
                )
                .gateway(takeOffToPointRequestDTO.getGateway())
                .execute();
        System.out.println("执行一件起飞");
    }

    @Override
    public void flightAuthorityGrab(FlightAuthorityGrabRequestDTO flightAuthorityGrabRequestDTO) throws MqttException {
        FlyServicesExecutor
                .create()
                .flightId(flightAuthorityGrabRequestDTO.getFlightId())
                .flightAuthorityGrab()
                .gateway(flightAuthorityGrabRequestDTO.getGateway())
                .execute();

    }

    @Override
    public void drcModeEnter(DrcModeEnterRequestDTO drcModeEnterRequestDTO) throws MqttException {
        FlyServicesExecutor
                .create()
                .drcModeEnter(drcModeEnterRequestDTO)
                .gateway(drcModeEnterRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void stickControl(StickControlRequestDTO stickControlRequestDTO) throws MqttException {
//        Integer roll,
//        Integer pitch,
//        Integer yaw,
//        Integer throttle,
//        Integer gimbalPitch
        Integer roll = defaultValue;
        Integer pitch = defaultValue;
        Integer yaw = defaultValue;
        Integer throttle = defaultValue;
        Integer gimbalPitch = defaultValue;

        switch (stickControlRequestDTO.getRoll()) {
            case 0:
                roll = defaultValue + step;
                break;
            case 1:
                roll = defaultValue - step;
                break;
            case 2:
                roll = defaultValue;
        }

        switch (stickControlRequestDTO.getYaw()) {
            case 0:
                yaw = defaultValue + step;
                break;
            case 1:
                yaw = defaultValue - step;
                break;
            case 2:
                yaw = defaultValue;
        }

        switch (stickControlRequestDTO.getPitch()) {
            case 0:
                pitch = defaultValue + step;
                break;
            case 1:
                pitch = defaultValue - step;
                break;
            case 2:
                pitch = defaultValue;
        }

        switch (stickControlRequestDTO.getThrottle()) {
            case 0:
                throttle = defaultValue + step;
                break;
            case 1:
                throttle = defaultValue - step;
                break;
            case 2:
                throttle = defaultValue;
        }

        switch (stickControlRequestDTO.getGimbalPitch()) {
            case 0:
                gimbalPitch = defaultValue + step;
                break;
            case 1:
                gimbalPitch = defaultValue - step;
                break;
            case 2:
                gimbalPitch = defaultValue;
        }
        System.out.println(roll + pitch + yaw + throttle + gimbalPitch);
        System.out.println(stickControlRequestDTO);
        roll = defaultValue + 300;
        pitch = defaultValue;
        yaw = defaultValue;
        throttle = defaultValue;
        gimbalPitch = defaultValue;
        FlyServicesExecutor
                .create()
                .stickControl(roll, pitch, yaw, throttle, gimbalPitch)
                .gateway(stickControlRequestDTO.getGateway())
                .drcExecute();
    }

    @Override
    public void droneControl(DroneControlRequestDTO droneControlRequestDTO) throws MqttException {
        System.out.println("droneControlGateway"+droneControlRequestDTO.getGateway());
        FlyServicesExecutor
                .create()
                .droneControl(droneControlRequestDTO.getX(), droneControlRequestDTO.getY(), droneControlRequestDTO.getH(), droneControlRequestDTO.getW())
                .gateway(droneControlRequestDTO.getGateway())
                .drcExecute();
    }


    @Override
    public void returnHome(ReturnHomeRequestDTO returnHomeRequestDTO) throws MqttException {
        FlyServicesExecutor
                .create()
                .flightId(returnHomeRequestDTO.getFlightId())
                .returnHome()
                .gateway(returnHomeRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void flyToPointUpdate(FlyToPointUpdateRequestDTO flyToPointUpdateRequestDTO) throws MqttException{
        System.out.println(flyToPointUpdateRequestDTO);
        FlyServicesExecutor.create()
                .flyToPointUpdate(flyToPointUpdateRequestDTO)
                .gateway(flyToPointUpdateRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void droneEmergencyStop(DroneEmergencyStopRequestDTO droneEmergencyStopRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .droneEmergencyStop()
                .gateway(droneEmergencyStopRequestDTO.getGateway())
                .drcExecute();
    }

    @Override
    public void flighttaskUndo(FlighttaskUndoRequestDTO flighttaskUndoRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .flighttaskUndo(flighttaskUndoRequestDTO.getFlightIds())
                .gateway(flighttaskUndoRequestDTO.getGateway())
                .drcExecute();
    }

//    @Override
//    public void liveStartPush(LiveStartPushRequestDTO liveStartPushRequestDTO) throws MqttException {
//        FlyServicesExecutor
//                .create()
//                .liveStartPush()
//                .gateway(liveStartPushRequestDTO.getGateway())
//                .execute();
//    }
}
